package com.ethink.system.service.impl;

import com.ethink.system.api.enums.SystemTodoTodoStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.system.api.info.SystemTodoInfo;
import com.ethink.system.api.request.SystemTodoAddRequest;
import com.ethink.system.api.request.SystemTodoListPendingRequest;
import com.ethink.system.api.request.SystemTodoUpdateRequest;
import com.ethink.system.api.response.SystemTodoAddResponse;
import com.ethink.system.common.entity.SystemTodo;
import com.ethink.system.common.mapper.SystemTodoMapper;
import com.ethink.system.converter.SystemTodoConverter;
import com.ethink.system.service.SystemTodoService;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 代办事项表 Service接口实现
 *
 * @author default
 * @date 2022-12-09
 */
@Slf4j
@Service
public class SystemTodoServiceImpl implements SystemTodoService {

    @Autowired
    private SystemTodoMapper systemTodoMapper;

    @Autowired
    private SystemTodoConverter systemTodoConverter;

    @Override
    public List<SystemTodoInfo> listSystemTodoPending(SystemTodoListPendingRequest request) {
        return systemTodoMapper.listSystemTodoPending(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SystemTodoAddResponse addSystemTodo(SystemTodoAddRequest request) {
        SystemTodoAddResponse response = new SystemTodoAddResponse();

        // 逻辑删除原DataId数据
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getDataId, request.getDataId());
        updateWrapper.eq(SystemTodo::getTodoType, request.getTodoType());
        updateWrapper.set(SystemTodo::getIsDeleted, true);

        SystemTodo systemTodo = new SystemTodo();
        BeanUtils.copyProperties(request, systemTodo);
        systemTodo.setTodoStatus(SystemTodoTodoStatusEnum.S1.getIntValue());
        systemTodoMapper.insert(systemTodo);
        response.setId(systemTodo.getId());
        return response;
    }

    @Override
    public void updateSystemTodo(SystemTodoUpdateRequest request) {
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getId, request.getId());
        updateWrapper.set(SystemTodo::getUserCode, request.getUserCode());
        updateWrapper.set(SystemTodo::getUserName, request.getUserName());
        updateWrapper.set(SystemTodo::getDataId, request.getDataId());
        updateWrapper.set(SystemTodo::getTodoType, request.getTodoType());
        updateWrapper.set(SystemTodo::getTodoStatus, request.getTodoStatus());
        updateWrapper.set(SystemTodo::getTodoMessage, request.getTodoMessage());
        updateWrapper.set(SystemTodo::getFinishTime, request.getFinishTime());
        systemTodoMapper.update(null, updateWrapper);
    }

    @Override
    public void doneSystemTodo(Long systemTodoId) {
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getId, systemTodoId);
        updateWrapper.set(SystemTodo::getTodoStatus, SystemTodoTodoStatusEnum.S2.getIntValue());
        updateWrapper.set(SystemTodo::getFinishTime, new Date());
        systemTodoMapper.update(null, updateWrapper);
    }

    @Override
    public void doneSystemTodoByDataId(Integer todoType, String dataId) {
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getDataId, dataId);
        updateWrapper.eq(SystemTodo::getTodoType, todoType);
        updateWrapper.set(SystemTodo::getTodoStatus, SystemTodoTodoStatusEnum.S2.getIntValue());
        updateWrapper.set(SystemTodo::getFinishTime, new Date());
        systemTodoMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteSystemTodo(Long systemTodoId) {
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getId, systemTodoId);
        updateWrapper.set(SystemTodo::getIsDeleted, true);
        systemTodoMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteSystemTodoByDataId(String systemTodoDataId) {
        LambdaUpdateWrapper<SystemTodo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemTodo::getDataId, systemTodoDataId);
        updateWrapper.set(SystemTodo::getIsDeleted, true);
        systemTodoMapper.update(null, updateWrapper);
    }

}
